package cn.zhihong.controller;

import cn.zhihong.common.R;
import cn.zhihong.pojo.AddressBook;
import cn.zhihong.service.AddressBookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName AddressBookController
 * @Author 庄志宏
 * @Date 2022/4/26 16:59
 */
@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    /**
     * @author 庄志宏
     * @date: 2022/4/26 19:18
     * 新增地址簿
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        log.info("新增地址簿{}",addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/26 20:22
     * 查询地址簿
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(addressBookLambdaQueryWrapper);
        return R.success(list);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/26 20:46
     * 修改地址中的回显地址
     */
    @GetMapping("/{id}")
    public R<AddressBook> huixian(@PathVariable("id") Long id){
        AddressBook addressBook = addressBookService.getById(id);
        log.info("性别为:{}",addressBook);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/26 20:53
     * 修改地址
     */
    @PutMapping
    public R<AddressBook> update(@RequestBody AddressBook addressBook){
        log.info("修改的地址为：{}",addressBook);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/26 21:12
     * 删除地址
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("地址的id为：{}",ids);
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

    /**
     * @author 庄志宏
     * @date: 2022/4/26 23:30
     * 设置默认地址
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook,HttpSession session){
        Long userId = (Long)session.getAttribute("user");
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        addressBookLambdaUpdateWrapper.eq(AddressBook::getUserId,userId);
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(addressBookLambdaUpdateWrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/27 19:31
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> selectDefault(HttpSession session){
        log.info("查询默认地址====");
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        return R.success(addressBook);
    }
}
